package com.baidu.location;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import java.util.ArrayList;
import java.util.Iterator;

public final class i
{
  private String a = "baidu_location_service";
  private ArrayList b = null;
  private float c = 3.4028235E+38F;
  private a d = null;
  private long e = 0L;
  private e f = null;
  private Context g = null;
  private int h = 0;
  private long i = 0L;
  private boolean j = false;
  private boolean k = false;
  private PendingIntent l = null;
  private AlarmManager m = null;
  private b n = null;
  private aj o = new aj(this);
  private boolean p = false;

  public i(Context paramContext, e parame)
  {
    this.g = paramContext;
    this.f = parame;
    this.f.a(this.o);
    this.m = ((AlarmManager)this.g.getSystemService("alarm"));
    this.n = new b(this);
    this.p = false;
  }

  private void a(long paramLong)
  {
    if (this.j)
      this.m.cancel(this.l);
    this.l = PendingIntent.getBroadcast(this.g, 0, new Intent("android.com.baidu.location.TIMER.NOTIFY"), 134217728);
    this.m.set(0, System.currentTimeMillis() + paramLong, this.l);
    ak.e();
  }

  private void b()
  {
    int i1;
    int i2;
    Iterator localIterator;
    int i4 = 10000;
    int i3 = 1;
    if ((this.b == null) || (this.b.isEmpty()))
      i2 = 0;
    do
    {
      if (i2 != 0)
        break label76;
      return;
      localIterator = this.b.iterator();
      i1 = 0;
      label42: i2 = i1;
    }
    while (!(localIterator.hasNext()));
    if (((d)localIterator.next()).i < 3)
      i1 = 1;
    while (true)
    {
      break label42:
      if (this.c > 5000.0F)
      {
        label76: i1 = 600000;
        if (!(this.k))
          break label214;
        this.k = false;
        i1 = i4;
      }
      label214: 
      while (true)
        while (true)
        {
          while (true)
          {
            while (true)
            {
              do
              {
                i2 = i3;
                if (this.h == 0)
                  continue;
                long l1 = this.i;
                long l2 = this.h;
                long l3 = System.currentTimeMillis();
                i2 = i3;
                if (i1 <= l1 + l2 - l3)
                  continue;
                i2 = 0;
              }
              while (i2 == 0);
              this.h = i1;
              this.i = System.currentTimeMillis();
              a(this.h);
              return;
              if (this.c <= 1000.0F)
                break;
              i1 = 120000;
            }
            if (this.c <= 500.0F)
              break;
            i1 = 60000;
          }
          i1 = 10000;
        }
    }
  }

  public final int a(d paramd)
  {
    Object localObject;
    if (this.b == null)
      this.b = new ArrayList();
    this.b.add(paramd);
    paramd.j = true;
    paramd.k = this;
    if (!(this.p))
    {
      this.g.registerReceiver(this.n, new IntentFilter("android.com.baidu.location.TIMER.NOTIFY"));
      this.p = true;
    }
    if (paramd.f == null)
      return 1;
    if (!(paramd.f.equals("gcj02")))
    {
      localObject = Jni.a(paramd.c, paramd.b, paramd.f + "2gcj");
      paramd.h = localObject[0];
      paramd.g = localObject[1];
      new StringBuilder().append(paramd.f).append("2gcj");
      ak.e();
      new StringBuilder("coor:").append(paramd.c).append(",").append(paramd.b).append(":").append(paramd.h).append(",").append(paramd.g);
      ak.e();
    }
    if ((this.d == null) || (System.currentTimeMillis() - this.e > 30000L))
      this.f.a();
    while (true)
    {
      b();
      return 1;
      localObject = new float[1];
      Location.distanceBetween(this.d.b(), this.d.c(), paramd.g, paramd.h, localObject);
      float f1 = localObject[0] - paramd.d - this.d.e();
      if (f1 > 0.0F)
      {
        if (f1 >= this.c)
          continue;
        this.c = f1;
      }
      if (paramd.i >= 3)
        continue;
      paramd.i += 1;
      paramd.a();
      if (paramd.i >= 3)
        continue;
      this.k = true;
    }
  }

  public final void a()
  {
    if (this.j)
      this.m.cancel(this.l);
    this.d = null;
    this.e = 0L;
    if (this.p)
    {
      ak.e();
      this.g.unregisterReceiver(this.n);
    }
    this.p = false;
  }

  public final int b(d paramd)
  {
    if (this.b == null)
      return 0;
    if (this.b.contains(paramd))
      this.b.remove(paramd);
    if ((this.b.size() == 0) && (this.j))
      this.m.cancel(this.l);
    return 1;
  }
}